package com.ycx.furns.dao.impl;

import com.ycx.furns.dao.BasicDAO;
import com.ycx.furns.dao.FurnDAO;
import com.ycx.furns.entity.Furn;
import com.ycx.furns.entity.Page;

import java.util.List;

public class FurnDAOImpl extends BasicDAO<Furn> implements FurnDAO {



    @Override
    public int addStockById(int id, int count) {
        String sql = "UPDATE furn SET stock = (stock + ?) WHERE id = ?";
        return update(sql, count, id);
    }

    @Override
    public int minusStockById(int id, int count) {
        String sql = "UPDATE furn SET stock = (stock - ?) WHERE id = ?";
        return update(sql, count, id);
    }

    @Override
    public int addSalesById(int id, int count) {
        String sql = "UPDATE furn SET sales = (sales + ?), stock = (stock - ?) WHERE id = ?";
        return update(sql, count, count, id);
    }

    @Override
    public int minusSalesById(int id, int count) {
        String sql = "UPDATE furn SET sales = (sales - ?), stock = (stock + ?) WHERE id = ?";
        return update(sql, count, count, id);
    }

    @Override
    public List<Furn> queryFurns() {
        String sql = "SELECT `id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path` FROM furn";
        return queryMulti(sql, Furn.class);
    }

    @Override
    public List<Furn> queryFurnsOfLimitByName(String name, int pageNo, int pageSize) {
        String sql = "SELECT `id` , `name` , `maker` , `price` , `sales` , `stock` , " +
                "`img_path` FROM furn WHERE `name` like ? limit ?, ?";
        return queryMulti(sql, Furn.class, "%"+name+"%", (pageNo - 1) * pageSize, pageSize);
    }

    @Override
    public int addFurn(Furn furn) {
        String sql = "INSERT INTO furn(`id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path`)" +
                "VALUES(NULL , ? , ? , ? , ? , ? , ?)";
        return update(sql, furn.getName(), furn.getMaker(), furn.getPrice(),
                furn.getSales(), furn.getStock(), furn.getImg_path());
    }

    @Override
    public int delFurn(Integer id) {
        String sql = "delete from furn where `id` = ?";
        return update(sql, id);
    }

    @Override
    public Furn getFurnById(Integer id) {
        String sql = "SELECT `id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path` FROM furn WHERE id = ? ";
        return querySingle(sql, Furn.class, id);
    }

    @Override
    public int updateFurn(Furn furn) {
        String sql = "UPDATE furn SET `name` = ?, maker=?, price=?, sales=?, stock=? WHERE id=?";
        return update(sql, furn.getName(), furn.getMaker(), furn.getPrice(), furn.getSales(),furn.getStock(), furn.getId());
    }

    @Override
    public List<Furn> queryFurnsOfLimit(int pageNo, int pageSize) {
        String sql = "SELECT `id`, `name` , `maker` , `price` , `sales` , `stock` , `img_path` FROM furn limit ? , ?";
        return queryMulti(sql, Furn.class, (pageNo - 1) * pageSize, pageSize);
    }

    @Override
    public int getTotalRowNum() {
        String sql = "SELECT COUNT(*) FROM furn";
        return ((Number)queryScalar(sql)).intValue();

    }

    @Override
    public int getTotalRowNumByName(String name) {
        String sql = "SELECT COUNT(*) FROM furn WHERE `name` LIKE ?";
        return ((Number)queryScalar(sql, "%"+name+"%")).intValue();
    }
}
